package com.filldream.fastboot.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.filldream.fastboot.base.entity.SysDept;
import com.filldream.fastboot.base.entity.SysPost;
import com.filldream.fastboot.base.mapper.SysPostMapper;
import com.filldream.fastboot.base.request.pc.sysPost.AddSysPostSTO;
import com.filldream.fastboot.base.request.pc.sysPost.ModifySysPostSTO;
import com.filldream.fastboot.base.request.pc.sysPost.SysPostPageSTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.service.SysPostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(rollbackFor = Exception.class)
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Override
    public B savePost(AddSysPostSTO addPostSTO) {
        SysPost sysPost = TransferUtil.changeToObj(addPostSTO, SysPost.class);
        sysPost.setCreateUser( LoginHolder.getPcUser().getUsername() );
        sysPost.setLinkId( LoginHolder.getPcUser().getLinkId() );
        checkSysPostCode(sysPost,null);
        boolean insert = save(sysPost);
        return B.checkSuccess(insert);
    }

    @Override
    public P<SysPost> pageList(SysPostPageSTO sysPostPageSTO) {

        QueryWrapper<SysPost> sql = new QueryWrapper<>();
        if( !LoginHolder.getPcUser().getLinkId().equals(0L) ){
            sql.eq( "link_id",LoginHolder.getPcUser().getLinkId() );
        }
        if( StringUtils.isNotBlank(sysPostPageSTO.getPostCode()) ){
            sql.like( "post_code",sysPostPageSTO.getPostCode() );
        }
        if( StringUtils.isNotBlank(sysPostPageSTO.getPostName()) ){
            sql.like( "post_name",sysPostPageSTO.getPostName() );
        }
        if( sysPostPageSTO.getPostType() != null ){
            sql.eq( "post_type",sysPostPageSTO.getPostType() );
        }
        if( sysPostPageSTO.getStatus() != null ){
            sql.eq( "status",sysPostPageSTO.getStatus() );
        }
        sql.last(" order by post_rank asc,create_time desc");

        Page<SysPost> page = page(sysPostPageSTO.page(), sql);
        return P.autoSuccess(page);
    }

    @Override
    public B modifyPost(ModifySysPostSTO modifySysPostSTO) {
        SysPost sysPost = TransferUtil.changeToObj(modifySysPostSTO, SysPost.class);
        checkSysPostCode(sysPost, modifySysPostSTO.getPostId());
        sysPost.setUpdateUser( LoginHolder.getPcUser().getUsername() );
        updateById(sysPost);
        return B.success();
    }

    private void checkSysPostCode(SysPost sysPost ,Long selfId) {
        QueryWrapper<SysPost> sql = new QueryWrapper<>();
        if( sysPost.getLinkId() != null ){
            sql.eq("link_id", sysPost.getLinkId());
        }

        if(StringUtils.isNotBlank(sysPost.getPostCode())){
            sql.eq("post_code", sysPost.getPostCode());
        }

        if( selfId != null ){
            sql.ne( "post_id",selfId );
        }

        Assert.isBoolean(count(sql) > 0, ErrorCode.BASE_ERR.reMsg( "编码 " + sysPost.getPostCode() + " 已存在"));
    }
}
